﻿using System;
using System.IO;
using System.Windows.Forms;
using ManagedVC1;

namespace TestEncodeApp
{
    public partial class Form1 : Form
    {
        private VC1Encoder m_enc;

        public Form1()
        {
            InitializeComponent();
            progressBar1.Maximum = 100;
            progressBar1.Minimum = 0;

            markInBox.Text = "00:00:01";
            markOutBox.Text = "00:00:10";
            filePathBox.Text = "";
            progressBar1.Enabled = false;
            cancelButton.Enabled = false;
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filePathBox.Text = openFileDialog1.FileName;
            }
        }

        private void encodeButton_Click(object sender, EventArgs e)
        {
            DoEncode();
        }

        private void DoEncode()
        {
            if (m_enc == null)
            {
                m_enc = new VC1Encoder();
                MediaSource source = new MediaSource();
                source.FileName = filePathBox.Text;
                source.MarkIn = TimeSpan.Parse(markInBox.Text);
                source.MarkOut = TimeSpan.Parse(markOutBox.Text);
                source.IncludeAudio = audioCheckBox.Checked;
                source.IsInterlaced = true;
                m_enc.Sources.Add(source);

                m_enc.ProfilePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "video_editor.prx");
                m_enc.OutputFile = "testencode.wmv";

                m_enc.BitRate = 1100;
                m_enc.ComplexityLevel = 2;
                m_enc.FrameRate = 29.97;
                m_enc.InterlacedSource = false;
                m_enc.MaxWidth = 720;
                m_enc.MaxHeight = 480;
                m_enc.MaxKeyFrameDistance = 30;
                m_enc.NumOfBFrames = 1;
                m_enc.Profile = 2;
                m_enc.QP = 31;
                //m_enc.VideoType = 2;
                m_enc.RateControlMode = 0;
                m_enc.VBVBufferInBytes = 1000 * 3 * 125;
                //m_enc.PeakBitRate = 1700;
                //m_enc.UseInLoopFilter = true;
                //m_enc.UseOverlapSmoothingFilter = true;
                //m_enc.UseDenoiseFilter = true;
                //m_enc.MotionSearchLevel = 4;
                //m_enc.MotionSearchMethod = 2;
                //m_enc.MVRange = 4;
                m_enc.LookAhead = true;

                m_enc.EncodingFailed += new EventHandler<EncoderFailedEventArgs>(m_enc_EncodingFailed);
                m_enc.ProgressChanged += new EventHandler<EncoderProgressEventArgs>(enc_ProgressChanged);
                m_enc.EncodingCompleted += new EventHandler(enc_EncodingCompleted);
                progressBar1.Enabled = true;
                cancelButton.Enabled = true;
            }

            m_enc.Encode();
        }

        void m_enc_EncodingFailed(object sender, EncoderFailedEventArgs e)
        {
            IAsyncResult result = progressBar1.BeginInvoke(new Action<object>(delegate(object tag)
            {
                progressBar1.Value = 0;
                progressBar1.Enabled = false;
                cancelButton.Enabled = false;
                MessageBox.Show("Encode Failed");
            }), new object());
        }

        void enc_EncodingCompleted(object sender, EventArgs e)
        {
            IAsyncResult result = progressBar1.BeginInvoke(new Action<object>(delegate(object tag)
            {
                progressBar1.Value = 0;
                progressBar1.Enabled = false;
                cancelButton.Enabled = false;
                if (m_enc.IsCancelled)
                {
                    MessageBox.Show("Encode Cancelled");
                }
            }), new object());
        }

        void enc_ProgressChanged(object sender, EncoderProgressEventArgs e)
        {
            if (progressBar1.Value != (int)e.PercentComplete)
            {
                IAsyncResult result = progressBar1.BeginInvoke(new Action<int>(delegate(int tag)
                    {
                        progressBar1.Value = tag;
                    }), (int)e.PercentComplete);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            m_enc.CancelEncode();
            cancelButton.Enabled = false;
        }
    }
}
